// pages/set/set.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    brand: [],
    isMB: false,
    type: "",
    factoryGroup:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  deMB() {
    this.setData({
      isMB: false
    })
  },
  add(e) {
    console.log(e)
    this.setData({
      isMB: true,
      type: e.currentTarget.dataset.type
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getList()
    this.getFactoryGroup()
  },
  getList() {
    var that = this
    wx.request({
      url: app.globalData.api + 'system/getSystemSetting',
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            brand: JSON.parse(res.data.data.brandList)
          })
        }
      }
    })
  },
  getFactoryGroup(){
    var that = this
    wx.request({
      url: app.globalData.api + 'factoryGroup/list',
      data: {
        rows:99
      },
      method:'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            factoryGroup: res.data.data.factoryGroupList
          })
        }else{
          that.setData({
            factoryGroup: []
          })
        }
      }
    })
  },
  z_Submit(e) {
    var that = this
    if (this.data.type === '工厂') {
      wx.request({
        url: app.globalData.api + 'factoryGroup/add',
        data: {
          groupName: e.detail.value.add
        },
        method: 'POST',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success(res) {
          console.log(res)
          if (res.data.code == 200) {
            that.setData({
              isMB: false
            })
            that.getFactoryGroup()
          }
        }
      })
    } else {
      this.data.brand.push(e.detail.value.add)
      wx.request({
        url: app.globalData.api + 'system/updateSystemSetting',
        data: {
          brandList: JSON.stringify(this.data.brand)
        },
        method: 'PUT',
        header: {
          Authorization: wx.getStorageSync('token')
        },
        success(res) {
          console.log(res)
          if (res.data.code == 200) {
            that.setData({
              isMB: false
            })
            that.getList()
          }
        }
      })
    }

  },
  z_dele(e) {
    console.log(e)
    this.data.brand.splice(e.currentTarget.dataset.index, 1)
    var that = this
    wx.request({
      url: app.globalData.api + 'system/updateSystemSetting',
      data: {
        brandList: JSON.stringify(this.data.brand)
      },
      method: 'PUT',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            isMB: false
          })
          that.getList()
        }
      }
    })
  },
  f_dele(e){
    var that = this
    wx.request({
      url: app.globalData.api + 'factoryGroup/' + e.currentTarget.dataset.id,
      data: {
        
      },
      method: 'DELETE',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            isMB: false
          })
          that.getFactoryGroup()
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})